package com.jintoufs.util.msg;

import com.alibaba.fastjson.JSON;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.profile.DefaultProfile;
import com.aliyuncs.profile.IClientProfile;
import com.aliyuncs.sms.model.v20160927.SingleSendSmsRequest;
import com.aliyuncs.sms.model.v20160927.SingleSendSmsResponse;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Value;

import java.util.Map;

/**
 * Created by Zhao Kai on 2017/2/6.
 * 短信发送实现类，完成短信发送动作
 */
public class SendSMS implements ISendSMS {

    private final static Logger log = Logger.getLogger(SendSMS.class);

    @Value("${aliyun_sms_region_id}")
    private static String aliyun_sms_region_id;    //阿里云regionId
    @Value("${aliyun_sms_access_key_id}")
    private static String aliyun_sms_access_key_id;    //阿里云 accessKeyId
    @Value("${aliyun_sms_access_key_secret}")
    private static String aliyun_sms_access_key_secret;    //阿里云 accessKeySecret
    @Value("${aliyun_sms_end_point_name}")
    private static String aliyun_sms_end_point_name;   //阿里云end_point_name
    @Value("${aliyun_sms_product}")
    private static String aliyun_sms_product;  //阿里云产品
    @Value("${aliyun_sms_domain}")
    private static String aliyun_sms_domain;   //阿里云调用短信服务平台接口域名

    private String signName;    //阿里云短信签名
    private String templateCode;    //阿里云短信模板Code
    private String paramString; //阿里云短信模板参数字符串
    private String recNum;  //发送短信手机号

    /**
     * @param signName     如果signName为null或者空串，则取集团短信签名; 否则取signName值
     * @param templateCode 短信模板CODE
     * @param paramMap     短信模板中参数名为key,参数值为value
     * @param phoneNum     手机号数组,例：{"18888888888","19999999999"}
     */
    public SendSMS(String signName, String templateCode, Map<String, String> paramMap, String[] phoneNum) {
        super();
        //短信签名: 如果signName为null或者空串，则取集团短信签名; 否则去signName值
        this.signName = (null == signName || "".equals(signName)) ? "上海弘泰" : signName;
        //短信模板
        this.templateCode = templateCode;
        //模板参数
        this.paramString = JSON.toJSONString(paramMap);
        //构造手机号字符串，多个手机号之间逗号分隔
        StringBuffer recNumSB = new StringBuffer();
        for (int i = 0; i < phoneNum.length; i++) {
            if (i == phoneNum.length - 1) {
                recNumSB.append(phoneNum[i]);
            } else {
                recNumSB.append(phoneNum[i] + ",");
            }
        }
        this.recNum = recNumSB.toString();
    }

    /**
     * @return 发送成功返回 true,否则返回 false
     * @description 发送短信
     */
    @Override
    public boolean sendSMS() {
        boolean isSendSuccess = false;
        try {
            IClientProfile profile = DefaultProfile.getProfile(aliyun_sms_region_id, aliyun_sms_access_key_id, aliyun_sms_access_key_secret);
            DefaultProfile.addEndpoint(aliyun_sms_end_point_name, aliyun_sms_region_id, aliyun_sms_product, aliyun_sms_domain);
            IAcsClient client = new DefaultAcsClient(profile);
            SingleSendSmsRequest request = new SingleSendSmsRequest();
            request.setSignName(signName);
            request.setTemplateCode(templateCode);
            request.setParamString(paramString);
            request.setRecNum(recNum);
            SingleSendSmsResponse response = client.getAcsResponse(request);//短信发送
            isSendSuccess = true;
        } catch (Exception e) {
            e.printStackTrace();
            log.error("*aliyun_sms_begin*");
            log.error(e.getMessage());
            log.error("*aliyun_sms_end*");
        } finally {
            return isSendSuccess;
        }
    }
}
